# coding=utf-8
import urllib.request
import socket
socket.setdefaulttimeout(10)

def downFile(u,saveFile,referer=""):
    print(u)
    try:
        user_agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36)'
        headers = { 'User-Agent' : user_agent,"Referer":referer }
        req = urllib.request.Request(url=u,  headers=headers)
        sock = urllib.request.urlopen(req)
        bs=sock.read()
        sock.close()
        f=open(saveFile,mode='wb')
        f.write(bs)
        f.close()
    except Exception  as e:
        print (e)
        return ""

def downHTML(u,encode="utf-8",referer=""):
    print(u)
    try:
        user_agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36)'
        headers = { 'User-Agent' : user_agent,"Referer":referer }
        req = urllib.request.Request(url=u,  headers=headers)
        sock = urllib.request.urlopen(req,timeout=5)
        html = sock.read().decode(encode,"ignore")
        sock.close()
    except Exception  as e:
        print (e)
        return ""
    return html